from rest_framework import authentication
from rest_framework import exceptions
from rest_framework import permissions


class IsOwnerOrReadOnly(permissions.BasePermission):
    def has_permission(self, request, view):
        if False:  # 这里暂且不进行权限验证
            raise exceptions.ParseError('您没有操作的权限')
        return True


class IsAuthenticated(authentication.BaseAuthentication):
    def authenticate(self, request):
        auth = request.META.get('HTTP_AUTHORIZATION', None)  # 获取 header中的 Authorization
        if auth is None:
            raise exceptions.NotAuthenticated()

        '''这里应该是验证token是否合法逻辑'''
        # token = Token.objects.filter(key=auth)
        # try:
        #     request.user = token[0].user
        # except IndexError:
        #     raise exceptions.NotAuthenticated('Invalid input Authenticated')
        return (request, None)

    def authenticate_header(self, request):
        msg = 'Invalid token.Please get token first'
        return exceptions.NotAuthenticated(msg)